package com.haohope.framework;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class FrameworkApp implements ApplicationContextAware{
	private static ApplicationContext applicationContext;
	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}
	public static Object getBean(String name) {
		try {
			return applicationContext.getBean(name);
		}catch(Exception e) {
			return null;
		}
	}
	@SuppressWarnings("rawtypes")
	public static Object getBean(Class<?> clazz){
		try {
			Map beans = applicationContext.getBeansOfType(clazz);
			if (null==beans) {
				return null;
			}
			return beans.values().iterator().next();
		} catch(Exception e) {
			return null;
		}
	}
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		FrameworkApp.applicationContext = applicationContext;
	}
}
